package com.woniuxy.service.location.impl;

import com.woniuxy.entity.City;
import com.woniuxy.mapper.CityMapper;
import com.woniuxy.service.location.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 马踏东京赏樱花
 */
@Service
public class CityServiceImpl implements CityService {
    @Autowired
    private CityMapper cityMapper;

    @Override
    public List<City> findAllCity(Integer provinceId) {
        return cityMapper.findAllCity(provinceId);
    }

    @Override
    public int addOne(String str) {
        City city = new City();
        city.setCityName(str);
        int insert = cityMapper.insert(city);
        if (insert == 1) {
            return insert;
        }
        throw new RuntimeException("添加失败");
    }

    @Override
    public int deleteOne(String str) {
        if (str == null) {
            throw new RuntimeException("数据获取失败");
        }
        City city = new City();
        city.setCityId(Integer.valueOf(str));
        return cityMapper.deleteByPrimaryKey(city.getCityId());
    }

    @Override
    public City findOne(String str) {
        if (str == null) {
            throw new RuntimeException("数据获取失败");
        }
        City city = new City();
        city.setCityId(Integer.valueOf(str));
        return cityMapper.selectByPrimaryKey(city.getCityId());
    }

    @Override
    public List<City> findAll() {
        return cityMapper.selectByExample(null);
    }

    @Override
    public City updateOne(City city) {
        int i = cityMapper.updateByPrimaryKey(city);
        if (i > 0) {
            return city;
        }
        throw new RuntimeException("修改失败");
    }
}
